/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.dao;

import java.io.File;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import etcore.webapp.grouptalk.common.object.Attachment;
import etcore.webapp.grouptalk.util.ConfigUtil;
import etcore.webapp.grouptalk.util.Symbols;

/**
 * An instance of AttachmentDAO is a data access object used to access
 * Attachment, which contains query and update operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class AttachmentDAO extends BaseDAO {

	@Override
	public void deleteSingle(Object persistentInstance) {

		super.deleteSingle(persistentInstance);
		if (persistentInstance instanceof Attachment) {
			String root = ConfigUtil.getReadRootPath();
			String str = root + Symbols.SLASH
					+ ((Attachment) persistentInstance).getAttached();
			File file = new File(str);
			if (file.isFile())
				file.delete();
		}

	}

	/**
	 * Find Attachment instances
	 * 
	 * @param begin
	 *            start position of finding instances
	 * @param length
	 *            count of instances to be obtained
	 * @return Attachment instances
	 */
	@SuppressWarnings("unchecked")
	public List<Attachment> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Attachment.class.getSimpleName());
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List<Attachment> results = queryObject.list();

		return results;

	}

	/**
	 * Find Attachment instance by id
	 * 
	 * @param id
	 *            Attachment ID
	 * @return Attachment instance
	 */
	public Attachment findById(java.lang.Integer id) {

		Session session = HibernateSessionFactory.getSession();
		Attachment instance = (Attachment) session.get(
				Attachment.class.getName(), id);

		return instance;

	}

	/**
	 * Find all instances of Attachment by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return Attachment instances
	 */
	@SuppressWarnings("unchecked")
	public List<Attachment> findByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();

		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Attachment.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append(" = ?  order by model.rankv");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);

		List<Attachment> results = queryObject.list();

		return results;

	}

	/**
	 * Find Attachment instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return Attachment instances
	 */
	@SuppressWarnings("unchecked")
	public List<Attachment> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Attachment.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append(" = ? order by id desc");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List<Attachment> results = queryObject.list();

		return results;

	}

	/**
	 * Find the count of Attachment instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return the count of Attachment instances found
	 */
	public long findSizeByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select count(*) from ");
		sqlStringBuffer.append(Attachment.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append(" = ? ");

		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

}